using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using AMPLibrary.Media;

namespace AMPLibrary.Collections
{
    public class NowPlayingCollection : Collection<MediaFile>, ISerializable
    {
        /// <summary>
        /// 
        /// </summary>
        private static NowPlayingCollection nowPlayingCollection;

        /// <summary>
        /// 
        /// </summary>
        private NowPlayingCollection()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static NowPlayingCollection GetNowPlayingCollection()
        {
            if (nowPlayingCollection == null)
                nowPlayingCollection = new NowPlayingCollection();

            return nowPlayingCollection;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool Serialize(string path)
        {
            Stream stream = null;
            BinaryFormatter bFormatter = new BinaryFormatter();

            try
            {
                if (File.Exists(path))
                {
                    try
                    {
                        File.Delete(path);
                    }
                    catch
                    {
                    }
                }

                stream = File.Open(path, FileMode.Create);

                System.Collections.ArrayList al = new System.Collections.ArrayList();

                foreach (MediaFile item in nowPlayingCollection)
                    al.Add(item);

                bFormatter.Serialize(stream, al);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool Deserialize(string path)
        {
            if (nowPlayingCollection.Count > 0)
                return true;

            Stream stream = null;
            BinaryFormatter bFormatter = new BinaryFormatter();

            try
            {
                stream = File.Open(path, FileMode.Open);
                System.Collections.ArrayList al = (System.Collections.ArrayList)bFormatter.Deserialize(stream);

                foreach (MediaFile item in al)
                    nowPlayingCollection.AddItem(MediaFile.LoadMedia(item.Path));
            }
            catch
            {
                return false;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return true;
        }
    }
}

